import {ChangeDetectorRef, Component} from '@angular/core';
import {LoadingController, NavController, NavParams} from 'ionic-angular';
import {JidihttpProvider} from "../../providers/jidihttp/jidihttp";

/**
 * Generated class for the ZuopinPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
  selector: 'page-zuopin',
  templateUrl: 'zuopin.html',
})
export class ZuopinPage {
  icons: string ="camera"
  pageno:number=1;
  zuopintype:string;
  type:string="list";
  /** zuopin **/
  zuopinlistitem:Array<any>=[];//list列表
  zuopinMoviesvaike: Array<any>=[];
  loading: any;
  infiniteEvent:any;//上拉加载的全局事件
  constructor(public navCtrl: NavController, public navParams: NavParams,public jidiProvider:JidihttpProvider,public cdchange:ChangeDetectorRef,public loadingCtrl:LoadingController) {
    this.zuppinhttpClinet();
  }

  ionViewDidLoad() {
    // console.log('ionViewDidLoad ZuopinPage');
  }

  /** http **/
  zuppinhttpClinet(){
    this.pageno =1;
    this.zuopintype = "create_time";
    this.jidiProvider.getZuoPin(this.pageno,this.zuopintype,this.type).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<data['data']['records']['length'];i++){
          this.zuopinMoviesvaike.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.zuopinlistitem=this.zuopinMoviesvaike;
        this.cdchange.detectChanges();
      }
    },error => {

    });


  }

  /** 分页请求 **/
  doInfinite(infinite){
    this.loading = this.loadingCtrl.create({
      content: '努力加载中...'
    });
    this.loading.present();


    this.infiniteEvent=infinite;
    this.pageno+=1;
    //任务请求
    this.jidiProvider.getZuoPin(this.pageno,this.zuopintype,this.type).subscribe(data => {
      if (this.loading) {
        this.loading.dismiss();
      }
      if(this.infiniteEvent){
        if(data['data']['records']['length']<1){
          this.infiniteEvent.enable(false);
        }
        for(let i=0;i<data['data']['records']['length'];i++){
          this.zuopinMoviesvaike.push(data['data']['records'][i]);
        }
        this.zuopinlistitem=this.zuopinMoviesvaike;
        this.infiniteEvent.complete();
        this.cdchange.detectChanges();
      }
    },error => {

    });
  }

  /** 最新上传 **/
  zuixinEvent(){
    this.zuopinlistitem=[];//list列表
    this.zuopinMoviesvaike=[];
    this.cdchange.detectChanges();
    this.pageno=1;
    this.zuopintype="create_time";
    if(this.infiniteEvent){
      this.infiniteEvent.enable(true);
    }
    this.jidiProvider.getZuoPin(this.pageno,this.zuopintype,this.type).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<data['data']['records']['length'];i++){
          this.zuopinMoviesvaike.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.zuopinlistitem=this.zuopinMoviesvaike;
        this.cdchange.detectChanges();
      }
    },error => {

    });

  }

  /** dianzan **/
  dianzanEvent(){
    this.zuopinlistitem=[];//list列表
    this.zuopinMoviesvaike=[];
    this.cdchange.detectChanges();
    this.pageno=1;
    this.zuopintype="giveLike";
    if(this.infiniteEvent){
      this.infiniteEvent.enable(true);
    }
    this.jidiProvider.getZuoPin(this.pageno,this.zuopintype,this.type).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<data['data']['records']['length'];i++){
          this.zuopinMoviesvaike.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.zuopinlistitem=this.zuopinMoviesvaike;
        this.cdchange.detectChanges();
      }
    },error => {

    });

  }

  /** huifu **/
  huifuEvent(){
    this.zuopinlistitem=[];//list列表
    this.zuopinMoviesvaike=[];
    this.cdchange.detectChanges();
    this.pageno=1;
    this.zuopintype="champion";
    if(this.infiniteEvent){
      this.infiniteEvent.enable(true);
    }
    this.jidiProvider.getZuoPin(this.pageno,this.zuopintype,this.type).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<data['data']['records']['length'];i++){
          this.zuopinMoviesvaike.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.zuopinlistitem=this.zuopinMoviesvaike;
        this.cdchange.detectChanges();
      }
    },error => {

    });

  }




}
